
#ifndef _HELPER_HPP
#define _HELPER_HPP

#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>

#include <map>
#include <vector>
#include <sstream>
#include <iostream>
#include <unistd.h>

#include "interface.hpp"
#include "fingerprint.hpp"

#define CNRM  "\x1B[0m"
#define CRED  "\x1B[31m"
#define CGRN  "\x1B[32m"
#define CYEL  "\x1B[33m"
#define CBLU  "\x1B[34m"
#define CMAG  "\x1B[35m"
#define CCYN  "\x1B[36m"
#define CWHT  "\x1B[37m"
#define CBRN  "\x1B[0;36m"

#define SM_LOG_ERR 1
#define SM_LOG_INFO 0

/* macros for getting sensor's value from packet */
#define get_contact(pkt, id) \
    ( (ntohs(*((uint16_t*)pkt->s)) >> ((id)-1)) & 0x1 )

#define get_button(pkt, id) \
    ( ( (ntohl(*((uint32_t*)(pkt)->b)) >> 8) >> (2*((id)-1)) ) & 0x3 )

enum {
    MODE,
    TIMER,
    FUNCTION
};

/* function for getting digital sensor's value */
double get_digital(p_packet_t *pkt, uint8_t id);

/* Convert integer data type to string */
std::string to_string(int d);

/* Convert float data type to string */
std::string to_string(float f);

/* Execute commands */
void exec(Commands_info &cmds, const std::string &value, const int &gid, const int &type);

/* Evaluate expr and return the value. */
double eval_expr(std::string expr, const std::string &value);

/* Tokenize the string */
void tokenize(const std::string& str,  const std::string& delimiters,
        std::vector<std::string>& tokens);

/* Find specified old_str in the str and replace it to new_str. */
void find_and_replace(std::string& str, const std::string& old_str, 
        std::string new_str);

/* Extract "g*" string from specified str. */
void extract_gids(const std::string str, std::vector<unsigned int>& cmds);

/* Replace the timers in the string (year, mon, day, hour, min, sec, mili). */
void replace_timers(std::string& s);

/* GSM */
int write_to_gsm(char *chars);
int read_from_gsm(char *result);

/* Handle client requests. */
std::string handle_request(std::string request);

/* parse serial config file and init serials. */
void parse_serial_config(std::string file_name);

/* initialize and start smarthome.*/
void init_and_start_core();

/* calculate check sum from the given base packet */
uint8_t calculate_csum(const b_packet_t* pkt);

/* convert string to packet structure */
b_packet_t* str_to_pkt(const std::string str);

/* convert packet to string structure */
std::string pkt_to_str(b_packet_t *pkt);

/* convert fingerprint packet to string structure */
std::string fp_pkt_to_str(fp_packet_t pkt);

/* add/remove user from config */
void add_user(std::string user_name, std::string id);
void remove_user(std::string id);
char * pthread_err_to_str(int err);

void replace_ids_to_values(std::vector<unsigned int> ids, std::string& str);
void logger(int level, const char* format, ...);

#endif // _HELPER_HPP
